package methodref02;

import org.junit.Test;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 方法引用使用的前提:
 *   引用的方法和Lambda表达式对应接口中的抽象方法的形参类型保持一致
 * 非静态成员引用格式
 *   对象名::方法名
 *
 */
public class MethodRefDemo02 {
	Person p = new Person("老王");

	@Test
	public void test01() {
		//Supplier<String> s = ()-> p.getName();
		Supplier<String> s = p::getName;
		System.out.println(s.get());
	}
	@Test
	public void test02() {
		//Consumer<String> c = name -> p.setName(name);
		Consumer<String> c = p::setName;
		c.accept("老李");
		System.out.println(p);
	}

	@Test
	public void test03() {
		String str="abc";
		//Supplier<Integer> s = ()-> str.length();
		Supplier<Integer> s = str::length;
		System.out.println(s.get());
	}

	@Test
	public void test04() {
		String str="abc";
		//Supplier<Character> s = () -> str.charAt(1);

		/*Supplier<Character> s = str::charAt;
		System.out.println(s.get());*/
	}
}
